package bb.lanxing.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.adapter.BaseListAdapter;


public class ShareView extends LinearLayout {
    public static final int TYPE_QQ = 0;
    public static final int TYPE_QZONE = 1;
    public static final int TYPE_WECHAT = 2;
    public static final int TYPE_WX_CIRCLE = 3;
    public static final int TYPE_WEIBO = 4;
    public static final int TYPE_CHAT = 5;
    public static final int TYPE_LOCAL = 6;
    public static final int TYPE_OTHER = 7;
    public static final int TYPE_WEB = 8;
    public static final int TYPE_COPY = 9;
    private ShareItemClickListener mListener;
    private ShareAdapter shareAdapter;
    public static final int[] DEFAULT_TYPES = {TYPE_QQ, TYPE_QZONE, TYPE_WECHAT, TYPE_WX_CIRCLE, TYPE_WEIBO, TYPE_LOCAL, TYPE_OTHER};
    private static final int[] icons = {
            R.drawable.umeng_socialize_qq, R.drawable.umeng_socialize_qzone, R.drawable.umeng_socialize_wechat,
            R.drawable.umeng_socialize_wxcircle, R.drawable.umeng_socialize_sina, R.drawable.ic_share_save_to_local,
            R.drawable.ic_share_save_to_local, R.drawable.ic_menu_more_gray, R.drawable.item_share_web,
            R.drawable.item_share_copy};
    private static final String[] textRes = App.getContext().getResources().getStringArray(R.array.share_items);

    public interface ShareItemClickListener {
        void onItemClick(int i);
    }

    public void setShareItemClickListener(ShareItemClickListener shareItemClickListener) {
        this.mListener = shareItemClickListener;
    }

    public ShareView(Context context) {
        super(context);
        init(context);
    }

    public ShareView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context);
    }

    public ShareView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        init(context);
    }

    public void buildType(int[] iArr) {
        ArrayList<ShareType> arrayList = new ArrayList<>(iArr.length);
        for (int i : iArr) {
            arrayList.add(new ShareType(i));
        }
        this.shareAdapter.update(arrayList, true);
    }

    public void init(Context context) {
        GridView gridView = (GridView) LayoutInflater.from(context).inflate(R.layout.share_view_layout, null);
        addView(gridView, new ViewGroup.LayoutParams(-1, -2));
        ShareAdapter shareAdapter = new ShareAdapter(context, new ArrayList<>(9));
        this.shareAdapter = shareAdapter;
        gridView.setAdapter(shareAdapter);
        buildType(DEFAULT_TYPES);
        gridView.setOnItemClickListener((adapterView, view, i, j) -> {
            ShareType item = ShareView.this.shareAdapter.getItem(i);
            if (ShareView.this.mListener != null) {
                ShareView.this.mListener.onItemClick(item.type);
            }
        });
    }

    public static class ShareType {
        int iconRes;
        String text;
        int type;

        public ShareType(int i) {
            this.type = i;
            this.text = ShareView.textRes[i];
            this.iconRes = ShareView.icons[i];
        }
    }

    public class ShareAdapter extends BaseListAdapter<ShareType> {
        public ShareAdapter(Context context, List<ShareType> list) {
            super(context, list);
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view == null) {
                view = LayoutInflater.from(this.mContext).inflate(R.layout.item_share_view, viewGroup, false);
                viewHolder = new ViewHolder();
                viewHolder.icon = view.findViewById(R.id.item_share_icon);
                viewHolder.msg = view.findViewById(R.id.item_share_text);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            ShareType item = getItem(i);
            viewHolder.icon.setImageResource(item.iconRes);
            viewHolder.msg.setText(item.text);
            return view;
        }

        static class ViewHolder {
            ImageView icon;
            TextView msg;
        }
    }
}
